//
//  RadioCollectionViewCell.m
//  NewMusic
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "RadioCollectionViewCell.h"
#import "CustomerNetworking.h"
#import "RadioDataCollectionViewCell.h"
#import "RadioListenModel.h"
#import "PlayerViewController.h"
#import "SongsAuditionListModel.h"
#import "MusicType.h"
@interface RadioCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,
    LeftSlideViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, assign) NSInteger rowCount;
@property (nonatomic, assign) NSInteger collectionViewSectionCount;
@property (nonatomic, retain) NSIndexPath *lastIndexPath;
@property (nonatomic, retain) NSArray *titleArray;
@property (nonatomic, retain) LeftSlideView *leftSliderView;
@property (nonatomic, retain) NSMutableArray *secondArray;
@end
@implementation RadioCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.array = [NSMutableArray array];
        self.secondArray = [NSMutableArray array];
        self.titleArray = @[@"热  门", @"场  景", @"年  代", @"心  情", @"语  言", @"风  格", @"特  色", @"人  群"];
        [self creatSubView];
        [self customer];
        [self buildCollectionView];
    }
    return self;
}
- (void)creatSubView
{
    self.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"backGroud" ofType:@"jpg"]]];
    
    self.leftSliderView = [[LeftSlideView alloc] initWithFrame:CGRectMake(0, 0, 80, self.contentView.frame.size.height) andArray:[NSArray arrayWithObjects:@"热门", @"场景", @"年代", @"心情", @"语言", @"风格", @"特色", @"人群", nil]];
    self.leftSliderView.delegate = self;
    [self.contentView addSubview:_leftSliderView];
    [_leftSliderView release];
}

- (void)customer
{
    NSString *string = @"http://fm.api.ttpod.com/radiolist?image_type=240_200&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5C&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=2b2eba0e983843cf5ebcfa76b63e1682221e74ef&idfa=C142307D-73E9-409F-88B5-2F7B28503941&utdid=VkKgUwkPrlsDAINyD9AzAIoe&alf=700606&bundle_id=com.ttpod.music&latitude=38.88255&longtitude=121.5398";
    
    [CustomerNetworking customerConnectWithURL:string andParameters:nil andBlock:^(id response) {
        NSDictionary *dictionary = response;
        for (NSDictionary *dic in [dictionary objectForKey:@"data"]) {
            RadioModel *radioModel = [RadioModel radioModelWithDictionary:dic];
            [self.array addObject:radioModel];
        }
        [self.collectionView reloadData];
    }];
}

- (void)buildCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(80, 0, self.frame.size.width - 80, self.frame.size.height) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    
    [self.collectionView registerClass:[RadioDataCollectionViewCell class] forCellWithReuseIdentifier:@"radioDataCollectionViewCell"];
    
    [self.collectionView registerClass:[RadioCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    [self.contentView addSubview:_collectionView];
    [_collectionView release];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        RadioCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        view.label.text = [self.titleArray objectAtIndex:indexPath.section];
        view.label.textAlignment = NSTextAlignmentRight;
        return view;
    }else{
        return nil;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(375, 50);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _array.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[[_array objectAtIndex:section] dataArray] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RadioDataCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"radioDataCollectionViewCell" forIndexPath:indexPath];
    cell.radioDataModel = [[[self.array objectAtIndex:indexPath.section] dataArray] objectAtIndex:indexPath.row];
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(15, 15, 0, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(125, 125);
}

//代理图片滑动
- (void)leftSlideViewButton:(UIButton *)button
{
    self.rowCount = button.tag - 1330;
    if (_array.count == 0) {
        
    }else{
        NSInteger section = button.tag - 1330;
        NSInteger item = 0;
        self.lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];
        [self.collectionView scrollToItemAtIndexPath:_lastIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([scrollView class] == [UICollectionView class]) {
        if (_collectionView.contentOffset.y == 0) {
            self.leftSliderView.number = 4;
        }else if (_collectionView.contentOffset.y > 250.0 && _collectionView.contentOffset.y < 500) {
            self.leftSliderView.number = 9;
        }else if (_collectionView.contentOffset.y > 1500.0 && _collectionView.contentOffset.y < 1700.0) {
            self.leftSliderView.number = 14;
        }else if (_collectionView.contentOffset.y > 1800 && _collectionView.contentOffset.y < 2000) {
            self.leftSliderView.number = 19;
        }else if (_collectionView.contentOffset.y > 2800.0 && _collectionView.contentOffset.y < 2930.0) {
            self.leftSliderView.number = 24;
        }else if (_collectionView.contentOffset.y > 3200.0 && _collectionView.contentOffset.y < 3400.0) {
            self.leftSliderView.number = 29;
        }else if (_collectionView.contentOffset.y > 4450.0 && _collectionView.contentOffset.y < 4500.0) {
            self.leftSliderView.number = 34;
        }else if (_collectionView.contentOffset.y > 4800.0 && _collectionView.contentOffset.y < 5000.0) {
            self.leftSliderView.number = 39;
        }else{
            
        }
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self cusCollection:[[[_array[indexPath.section] dataArray] objectAtIndex:indexPath.row] tag_id]];
}

- (void)cusCollection:(NSString *)string
{
    [CustomerNetworking customerConnectWithURL:[NSString stringWithFormat:@"http://fm.api.ttpod.com/vipradiosong?num=150&tagid=%@&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5C&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=2b2eba0e983843cf5ebcfa76b63e1682221e74ef&idfa=C142307D-73E9-409F-88B5-2F7B28503941&utdid=VkKgUwkPrlsDAINyD9AzAIoe&alf=700606&bundle_id=com.ttpod.music&latitude=38.88244&longtitude=121.54", string] andParameters:nil andBlock:^(id response) {
        NSDictionary *dictionary = response;
        [self.secondArray removeAllObjects];
        for (NSDictionary *dic in [dictionary objectForKey:@"data"]) {
            RadioListenModel *radio = [RadioListenModel radioListenModelWithDictionary:dic];
            if (radio.audition_list.count != 0) {
                [_secondArray addObject:radio];
            }
        }
        PlayerViewController *player = [PlayerViewController sharePlayerViewController];
        NSMutableArray *arr = [NSMutableArray array];
        
        for (NSInteger i = 0; i < self.secondArray.count; i++)
        {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setValue:[_secondArray[i] album_name] forKey:@"album"];// 专辑
            [dic setValue:[_secondArray[i] song_name] forKey:@"name"]; // 歌名
            NSMutableArray *array = [NSMutableArray array];
            for (SongsAuditionListModel *song in [self.secondArray[i] audition_list]) {
                MusicType *music = [[MusicType alloc] init];
                music.url = song.urlString;
                music.typeDescription = @"压缩品质";
                [array addObject:music];
                [music release];
            }
            [dic setValue:array forKey:@"music"]; // 歌曲,model用musicType
            [dic setValue:[_secondArray[i] singer_name] forKey:@"songerName"];// 歌手名
            [dic setValue:[_secondArray[i] song_id] forKey:@"id"]; // id
            [arr addObject:dic];
        }
        player.musicArr = arr;
        [player MusicStart:0];
    }];
}

@end
